import getmyapplytemplatelist from '@/services/flowMine/list';
import { dictionaryList } from '@/services/dictionary';
import { getTemplatelist } from '@/pages/WorkFlow/Flow/List/services';

export const countryDict = [
    {
        code: 'ORG15540490989436999',
        name: '集团',
        processInstanceId_own: 'GP-HRTP-VG-20200123-001',
        processInstanceId_other: 'GP-HRTP-VG-20200208-001',
        processInstanceId_leave: 'GP-HRLO-VG-20200309-001',
        processInstanceId_leaveOther: 'GP-HRLO-VG-20200309-002',
        processInstanceId_regular: 'GP-HRR-VG-20200309-001',
        unit: 'S$'
    },
    {
        code: 'ORG155404909894361009',
        name: '厦门',
        processInstanceId_own: 'GP-HRTP-XM-20200123-001',
        processInstanceId_other: 'GP-HRTP-XM-20200208-001',
        processInstanceId_leave: 'GP-HRLO-XM-20200309-001',
        processInstanceId_leaveOther: 'GP-HRLO-XM-20200309-002',
        processInstanceId_regular: 'GP-HRR-XM-20200309-001',
        unit: '¥'
    },
    {
        code: 'ORG155404909894361019',
        name: '新加坡',
        processInstanceId_own: 'GP-HRTP-SG-20200123-001',
        processInstanceId_other: 'GP-HRTP-SG-20200208-001',
        processInstanceId_leave: 'GP-HRLO-SG-20200309-001',
        processInstanceId_leaveOther: 'GP-HRLO-SG-20200309-002',
        processInstanceId_regular: 'GP-HRR-SG-20200309-001',
        unit: 'S$'
    },
    {
        id: 1005,
        name: '深圳',
        // processInstanceId_own: 'GP-HRTP-XM-20200123-001',
        // processInstanceId_other: 'GP-HRTP-XM-20200208-001',
        // processInstanceId_leave: 'GP-HRLO-XM-20200309-001',
        // processInstanceId_leaveOther: 'GP-HRLO-XM-20200309-002',
        // processInstanceId_regular: 'GP-HRR-XM-20200309-001',
        unit: '¥'
    }
];

// 添加员工调岗申请
const addPost = (user, currentCompany) => {
    if (currentCompany) {
        return {
            processInstanceId: currentCompany.processInstanceId_own,
            templateName: `调岗申请-${currentCompany.name}`,
            iconType: 'ownPost',
            type: 'post'
        };
    }
    throw new Error('找不到员工发起申请！');
};
// 添加为他人调岗申请
const addOtherPost = (user, currentCompany) => {
    if (currentCompany) {
        return {
            processInstanceId: currentCompany.processInstanceId_other,
            templateName: `他人调岗申请-${currentCompany.name}`,
            iconType: 'otherPost',
            isOtherPost: true,
            type: 'post'
        };
    }
    throw new Error('找不到员工发起申请！');
};

const addLeave = (user, currentCompany) => {
    if (currentCompany) {
        return {
            processInstanceId: currentCompany.processInstanceId_leave,
            templateName: `离职申请-${currentCompany.name}`,
            iconType: 'lzApply',
            type: 'leave'
        };
    }
    throw new Error('找不到员工发起申请！');
};

const addOtherLeave = (user, currentCompany) => {
    if (currentCompany) {
        return {
            processInstanceId: currentCompany.processInstanceId_leaveOther,
            templateName: `办理他人离职-${currentCompany.name}`,
            iconType: 'lzOtherApply',
            type: 'leave',
            isOtherPost: true
        };
    }
    throw new Error('找不到员工发起申请！');
};

const addRegular = (user, currentCompany) => {
    if (currentCompany) {
        return {
            processInstanceId: currentCompany.processInstanceId_regular,
            templateName: `转正申请-${currentCompany.name}`,
            iconType: 'regular',
            type: 'regular'
        };
    }
    throw new Error('找不到员工发起申请！');
};

// 添加其他入口
const addOtherEntry = (data, hasQuitHrAuth) => {
    const postIndex = data.findIndex(item => item.category === '2');
    const user = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {};
    const currentCompany = countryDict.find(item => item.code === user?.firstOrgInfo?.firstCompanyCode);
    // 调岗申请
    const post = addPost(user, currentCompany);
    // 他人调岗申请
    const otherPost = addOtherPost(user, currentCompany);
    // 离职
    const leave = addLeave(user, currentCompany);
    // 为他人离职
    const otherLeave = addOtherLeave(user, currentCompany);
    // 转正
    const regular = addRegular(user, currentCompany);

    if (postIndex !== -1) {
        return [
            ...data.slice(0, postIndex),
            {
                ...data[postIndex],
                myApplyTemplateVOList: [
                    ...data[postIndex].myApplyTemplateVOList,
                    regular,
                    post,
                    otherPost,
                    leave,
                    hasQuitHrAuth && otherLeave
                ].filter(item => item)
            },
            ...data.slice(postIndex + 1)
        ];
    }

    return [
        ...data,
        {
            category: '2',
            myApplyTemplateVOList: [regular, post, otherPost, leave, hasQuitHrAuth && otherLeave].filter(item => item)
        }
    ];
};

export default {
    namespace: 'mineList',
    state: {
        list: [],
        categoryType: [],
        tempList: []
    },
    effects: {
        *fetchCategoryType(_, { call, put }) {
            const result = yield call(dictionaryList, {
                code: 'categoryType'
            });

            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        categoryType: result.data
                    }
                });
            }
        },
        *fetchList({ payload }, { call, put }) {
            const { hasQuitHrAuth } = payload;
            const result = yield call(getmyapplytemplatelist, payload);
            const user = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {};

            // 为深圳公司1005专门去除多余的申请
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: { list: user.companyId !== 1005 ? addOtherEntry(result.data, hasQuitHrAuth) : result.data }
                });
            }
        },
        *fetchTemplatelist({ payload }, { call, put }) {
            const result = yield call(getTemplatelist, payload);
            console.log(result, 'ssssssssssssss');
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        tempList: result.data
                    }
                });
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
